package com.epam.task.web.dao;

import com.epam.task.web.app.ConnectionToDB;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;

/**
 * Created with IntelliJ IDEA.
 * User: zhunusbekova.arailym
 * Date: 27.09.13
 * Time: 11:23
 * To change this template use File | Settings | File Templates.
 */
public class DAO {
    /**
     * Created with IntelliJ IDEA.
     * User: zhunusbekova.arailym
     * Date: 25.09.13
     * Time: 17:40
     * To change this template use File | Settings | File Templates.
     */
    public static class ExecuteCallableStatementToDB {
        public static final Logger LOGGER = LoggerFactory.getLogger(ConnectionToDB.class);
        public static void main(String[] args) {
            Connection con = null;
            CallableStatement cs = null;
            ResultSet rs = null;
            try {
                Class.forName("org.gjt.mm.mysql.Driver");
                con = DriverManager.getConnection(
                        "jdbc:mysql://127.0.0.1/test", "root", "123456");
                cs = con.prepareCall("{call calcstudents(?)}");
                cs.registerOutParameter(1,java.sql.Types.INTEGER);
                cs.execute();
                int empName = cs.getInt(1);
                LOGGER.warn("Count students - " + empName);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (SQLException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }

    public static class ExecutePreparedStatement {
        public static void main(String[] args) {
            Connection con = null;
            Statement st = null;
            ResultSet rs = null;
            try {
                Class.forName("org.gjt.mm.mysql.Driver");
                con = DriverManager.getConnection(
                        "jdbc:mysql://127.0.0.1/test","root", "123456");
                        String sql = "INSERT INTO students(name,id_group) VALUES( ?,?)";
                PreparedStatement ps = con.prepareStatement(sql);
                insert(ps, "Кукушнин", 851001);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (SQLException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        static void insert(PreparedStatement ps, String name, int idGroup)
                throws SQLException {
            ps.setString(1, name);
            ps.setInt(2, idGroup);
            ps.executeUpdate();
        }
    }

    public static class ExecuteQueryToDB {
        public static final Logger LOGGER = LoggerFactory.getLogger(ConnectionToDB.class);
        public static void main(String[] args) {
            Connection con = null;
            Statement st = null;
            ResultSet rs = null;
            try {
                Class.forName("org.gjt.mm.mysql.Driver");
                con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/test",
                        "root", "");
                st = con.createStatement();
                rs = st.executeQuery("SELECT * FROM oc_product");
                while (rs.next()) {
                   LOGGER.info(rs.getInt(1) + " " + rs.getString(2) + "" + rs.getInt(3));
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (rs != null) rs.close();
                    if (st != null) st.close();
                    if (con != null) con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * Created with IntelliJ IDEA.
     * User: zhunusbekova.arailym
     * Date: 25.09.13
     * Time: 17:35
     * To change this template use File | Settings | File Templates.
     */

    public static class ExecuteUpdateToDB {
        public static final Logger LOGGER = LoggerFactory.getLogger(ConnectionToDB.class);
        public static void main(String[] args) {
            Connection con = null;
            Statement st = null;
            ResultSet rs = null;
            try {
                Class.forName("org.gjt.mm.mysql.Driver");
                con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/test","root", "123456");
                        st = con.createStatement();
                int countRows = st.executeUpdate(
                        "INSERT INTO oc_product (name, id_group) VALUES (\"Баба-Яга\",123456)");
                LOGGER.info(String.valueOf(countRows));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (SQLException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }
}
